package jzbank.controller;

import jzbank.common.responsTool.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import jzbank.pojo.vo.TaskInfo;
import jzbank.service.impl.TaskServiceImpl;

import java.util.List;

@RestController
@RequestMapping("/api")
@Api(tags = "任务管理模块")
public class TaskController {

    @Autowired
    private TaskServiceImpl service;

    /**
     * 添加任务信息
     *
     * @param taskInfo 任务信息
     * @return 成功/失败
     */

    @ApiOperation(value = "添加任务", notes = "添加任务信息")
    @PostMapping(value = "/create", consumes = "application/json", produces = "application/json")
    public Res create(@RequestBody TaskInfo taskInfo) {
        System.out.println("添加任务：" + taskInfo.toString());
        try {
            service.createJob(taskInfo);
            return Res.ok();
        } catch (Exception e) {
            System.out.println("报错了");
            return Res.error(e.getMessage());
        }
    }

    /**
     * 查询所有任务列表
     *
     * @return 任务列表
     */

    @ApiOperation(value = "查询所有任务", notes = "查询所有任务列表")
    @GetMapping("/list")
    public Res list() {
        System.out.println("查询任务列表");
        List<TaskInfo> list = service.list();
        return Res.ok(list);
    }

    /**
     * 启动任务
     *
     * @param id 任务id
     * @return 启动是否成功
     */

    @ApiOperation(value = "启动任务", notes = "启动任务")
    @ApiImplicitParam(name = "id", value = "任务ID", required = true, dataType = "string", paramType = "query")
    @PostMapping("/start")
    public Res start(@RequestParam String id) {
        System.out.println("启动任务：" + id);
        try {
            service.startJob(id);
            return Res.ok();
        } catch (Exception e) {
            return Res.error(e.getMessage());
        }
    }

    /**
     * 暂停任务
     *
     * @param id 任务id
     * @return 暂停成功
     */

    @ApiOperation(value = "暂停任务", notes = "暂停任务")
    @ApiImplicitParam(name = "id", value = "任务ID", required = true, dataType = "string", paramType = "query")
    @PostMapping("/pause")
    public Res pause(@RequestParam String id) {
        try {
            System.out.println("暂停任务" + id);
            service.pauseJob(id);
            return Res.ok();
        } catch (Exception e) {
            return Res.error(e.getMessage());
        }
    }

    /**
     * 恢复任务
     *
     * @param id 任务id
     * @return 成功
     */

    @ApiOperation(value = "恢复任务", notes = "恢复任务")
    @ApiImplicitParam(name = "id", value = "任务ID", required = true, dataType = "string", paramType = "query")
    @PostMapping("/resume")
    public Res resume(@RequestParam String id) {
        try {
            System.out.println("恢复任务" + id);
            service.resumeJob(id);
            return Res.ok();
        } catch (Exception e) {
            return Res.error(e.getMessage());
        }
    }

    /**
     * 删除任务
     *
     * @param id id
     * @return
     */

    @ApiOperation(value = "删除任务", notes = "删除任务")
    @ApiImplicitParam(name = "id", value = "任务ID", required = true, dataType = "string", paramType = "query")
    @PostMapping("/remove")
    public Res remove(@RequestParam String id) {
        System.out.println("删除任务：" + id);
        try {
            service.deleteJob(id);
            return Res.ok();
        } catch (Exception e) {
            return Res.error(e.getMessage());
        }
    }
}